package org.zhang.shortlink.admin.controller;

import lombok.RequiredArgsConstructor;
import org.redisson.api.RBloomFilter;
import org.springframework.web.bind.annotation.*;
import org.zhang.shortlink.admin.dto.req.UserLoginDTO;
import org.zhang.shortlink.admin.dto.req.UserRegisterDTO;
import org.zhang.shortlink.admin.dto.req.UserUpdateDTO;
import org.zhang.shortlink.admin.dto.resp.SysUserVo;
import org.zhang.shortlink.admin.service.SysUserService;
import org.zhang.shortlink.common.convention.result.Result;
import org.zhang.shortlink.common.convention.result.Results;

/**
 * 用户管理控制层
 */
@RestController
@RequiredArgsConstructor
public class UserController {


    private final SysUserService userService;

    private final RBloomFilter<String> userRegisterCachePenetrationBloomFilter;

    /**
     * 根据用户名获取用户信息
     */
    @GetMapping("/api/short-link/admin/v1/user/{username}")
    public Result<SysUserVo> getUserByUserName(@PathVariable String username){
        SysUserVo sysUserVo =userService.getUserByUsername(username);
        return Results.success(sysUserVo);
    }

    /**
     * 根据用户名查询用户是否存在
     */
    @GetMapping("/api/short-link/v1/admin/user/has-username")
    public Result<Boolean> hasUsername(@RequestParam("username") String username){
        return Results.success(userService.hasUsername(username));
    }

    /**
     * 用户注册
     */
    @PostMapping("/api/short-link/admin/v1/user")
    public Result<Void> register(@RequestBody UserRegisterDTO userRegisterDTO){
        userService.register(userRegisterDTO);
        return Results.success();
    }

    /**
     * 修改用户信息
     */
    @PutMapping("/api/short-link/admin/v1/user")
    public Result<Void> updateUser(@RequestBody UserUpdateDTO userUpdateDTO){
        userService.update(userUpdateDTO);
        return Results.success();
    }

    /**
     * 用户登录
     */
    @PostMapping("/api/short-link/admin/v1/user/login")
    public Result<String> login(@RequestBody UserLoginDTO userLoginDTO){
        return Results.success(userService.login(userLoginDTO));
    }

    /**
     * 用户退出登录
     */
    @DeleteMapping("/api/short-link/admin/v1/user/logout")
    public Result<Void> logout(@RequestParam("username") String username){
        return Results.success(userService.logout(username));
    }

}
